package com.evan.aiu.service;

import com.evan.aiu.dao.AnswerDAO;
import com.evan.aiu.entity.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerService {
    @Autowired
    AnswerDAO answerDAO;

    public Page list(int page, int size) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return answerDAO.findAll(PageRequest.of(page, size, sort));
    }

    public Answer findById(int id) {
        return answerDAO.findById(id);
    }

    public List<Answer> findByAid(int aid) {
        return answerDAO.findAllByAid(aid);
    }

    public void addOrUpdate(Answer answer) {
        answerDAO.save(answer);
    }

    public void delete(int id) {
        answerDAO.deleteById(id);
    }
}
